package com.woniuxy.creditsystem.systemmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.systemmag.service.IDataDictionaryService;
import com.woniuxy.creditsystem.systemmag.service.IProcessModelService;
import com.woniuxy.creditsystem.systemmag.util.ExportToExcel;
import com.woniuxy.creditsystem.systemmag.util.GetBinaryStream;
import com.woniuxy.creditsystem.systemmag.util.ImportExcel;
import com.woniuxy.creditsystem.systemmag.util.UploadImg;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处理流程模型模块的请求
 * @Author liaojian
 * */
@RestController
@RequestMapping("/proc")
public class ProcessModelConteoller {

    @Resource
    private IProcessModelService processModelServiceImpl;

    /**
     * 分页查询
     * @Author liaojian
     * @Return TableResult
     * */
    @GetMapping(value = "/page",produces = {"application/json;charset=utf-8"})
    public TableResult page(PageRequest request, ProcessModelInfo pro){
        System.out.println(request);
        System.out.println(pro);
        PageInfo<ProcessModelInfo> pageInfo = processModelServiceImpl.findAllByParams(request,pro);
        return new TableResult<ProcessModelInfo>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 删除数据
     * @Author liaojian
     * */
    @GetMapping(value = "/delete",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(long id){
        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);
        processModelServiceImpl.delete(id);
        return resultMsg;
    }

    /**
     * 上传图片
     * @Author liaojian
     * */
    @RequestMapping(value = "/importImg",produces = {"application/json;charset=utf-8"})
    public ResultMsg importData(@RequestParam("file") MultipartFile file) throws Exception{
        ResultMsg resultMsg = new ResultMsg(200,"上传成功",null);
        String fileName = UploadImg.getImgName(file);
        resultMsg.setData(fileName);
        return resultMsg;
    }

    /**
     * 新增数据
     * @Author liaojian
     * */
    @GetMapping(value = "/save",produces = {"application/json;charset=utf-8"})
    public ResultMsg save(ProcessModelInfo pro){
        pro.setNum(System.currentTimeMillis());
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        processModelServiceImpl.save(pro);
        return resultMsg;
    }

    /**
     * 获取资源二进制流数据
     * @Author liaojian
     * @Return 资源二进制流数据
     * */
    @RequestMapping("/getData")
    public ResponseEntity<byte[]> download(
            String fileName, @RequestHeader("User-Agent") String userAgent){
        return GetBinaryStream.getStream(fileName,userAgent);
    }

    /**
     * 修改数据
     * @Author liaojian
     * */
    @GetMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(ProcessModelInfo pro){
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        processModelServiceImpl.update(pro);
        return resultMsg;
    }

    /**
     * 数据导出
     * @Author liaojian
     * */
    @PostMapping(value = "/export",produces = {"application/json;charset=utf-8"})
    public ResultMsg export(@RequestBody List<ProcessModelInfo> lists) throws Exception{
        String fileName = ExportToExcel.getProFileName(lists);
        ResultMsg resultMsg = new ResultMsg(200,"导出成功",fileName);
        return resultMsg;
    }

    /**
     * 数据导入
     * @Author liaojian
     * */
    @RequestMapping(value = "/importExcel",produces = {"application/json;charset=utf-8"})
    public ResultMsg importExcel(@RequestParam("file") MultipartFile file) throws Exception{
        ResultMsg resultMsg = new ResultMsg(200,"导入成功",null);
        processModelServiceImpl.saveList(file);
        return resultMsg;
    }
}
